﻿using HandyControl.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Uipath_License.Model;
using Uipath_License.Tools;

namespace Uipath_License.Uc
{
    /// <summary>
    /// UC_InputNetActivate.xaml 的交互逻辑
    /// </summary>
    public partial class UC_InputNetActivate : UserControl
    {
        public UC_InputNetActivate()
        {
            InitializeComponent();
        }

        private void Activate_Button_Click(object sender, RoutedEventArgs e)
        {
            Activate_TextBlock.Text = "正在激活";
            ProgressButton button = sender as ProgressButton;

            button.IsChecked = true;
            button.IsEnabled = false;
            string licenseText = LicenseCode_Text.Text;
            Thread thread = new Thread(new ThreadStart(() =>
            {
                Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
                
                string res = uipath_LicenseHelper.Net_Activate(licenseText);
                if (res.Contains("ActiveLicense"))
                {
                    HandyControl.Controls.MessageBox.Success("激活成功");
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        button.IsChecked = false;
                        button.IsEnabled = true;
                        Activate_TextBlock.Text = "激活";
                    }));
                    return;
                }
                if (res.Contains("There is a license already"))
                {
                    MessageBoxResult mesbox = HandyControl.Controls.MessageBox.Show("当前本地存在License,是否继续激活？\n继续激活将停用本地存在的License！", "请确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (mesbox == MessageBoxResult.No)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            button.IsChecked = false;
                            button.IsEnabled = true;
                            Activate_TextBlock.Text = "一键激活";
                        }));
                        return;

                    }
                    string deactivate_res = uipath_LicenseHelper.Net_Deactivate();
                    //if (deactivate_res.Contains("NoLicenseAvailable"))ActiveLicense
                    //{

                    //}


                }
                res = uipath_LicenseHelper.Net_Activate(licenseText);
                if (res.Contains("ActiveLicense"))
                {
                    HandyControl.Controls.MessageBox.Success("激活成功");
                }
                else
                {
                    HandyControl.Controls.MessageBox.Error("激活失败");
                }
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    button.IsChecked = false;
                    button.IsEnabled = true;
                    Activate_TextBlock.Text = "激活";
                }));
            }));
            thread.Start();
        }
    }
}
